export const drag = (el: any) => {
  const elDiv = el; //当前元素
  elDiv.onmousedown = function (e: any) {
    e.preventDefault();
    const bw = document.body.clientWidth;
    const bh = document.body.clientHeight;
    //鼠标按下，计算当前元素距离可视区的距离
    const disX = e.clientX - elDiv.offsetLeft;
    const disY = e.clientY - elDiv.offsetTop;
    // 计算两边坐标
    document.onmousemove = function (e) {
      let left = 0,
        top = 0;
      // 拖动边界
      if (e.clientX >= bw) {
        left = bw - disX;
      } else if (e.clientX <= 0) {
        {
          left = 0 - disX;
        }
      } else {
        left = e.clientX - disX;
      }
      if (e.clientY >= bh) {
        top = bh - disY;
      } else if (e.clientY <= 0) {
        top = 0 - disY;
      } else {
        top = e.clientY - disY;
      }
      //移动当前元素
      elDiv.style.left = left + 'px';
      elDiv.style.top = top + 'px';
      elDiv.style.cursor = 'move';
    };
    // 鼠标停止移动时，事件移除
    document.onmouseup = function () {
      document.onmousemove = null;
      document.onmouseup = null;
    };
  };
};
